{% extends 'base.html' %}
{% load static %}

{% block title %}库存状态{% endblock %}

{% block extra_css %}
<style>
    .stats-card {
        border-left: 4px solid #007bff;
        transition: transform 0.2s;
    }
    .stats-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    }
    .stats-number {
        font-size: 2rem;
        font-weight: bold;
    }
    .stats-label {
        color: #6c757d;
        font-size: 0.9rem;
    }
    .filter-section {
        background: #f8f9fa;
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
    }
    .table-responsive {
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .status-badge {
        font-size: 0.8rem;
        padding: 4px 8px;
    }
    .stock-bar {
        height: 6px;
        border-radius: 3px;
        background: #e9ecef;
        overflow: hidden;
    }
    .stock-fill {
        height: 100%;
        transition: width 0.3s ease;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2><i class="fas fa-chart-bar me-2"></i>库存状态</h2>
                <div class="btn-group">
                    <a href="{% url 'assets:consumable_list' %}" class="btn btn-primary">
                        <i class="fas fa-boxes me-1"></i>耗材管理
                    </a>
                    <a href="{% url 'assets:inventory_alert_list' %}" class="btn btn-warning">
                        <i class="fas fa-exclamation-triangle me-1"></i>预警列表
                    </a>
                </div>
            </div>

            <!-- 统计卡片 -->
            <div class="row mb-4">
                <div class="col-md-3">
                    <div class="card stats-card h-100">
                        <div class="card-body text-center">
                            <div class="stats-number text-primary">{{ total_count }}</div>
                            <div class="stats-label">总耗材数</div>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card stats-card h-100">
                        <div class="card-body text-center">
                            <div class="stats-number text-success">{{ normal_count }}</div>
                            <div class="stats-label">库存正常</div>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card stats-card h-100">
                        <div class="card-body text-center">
                            <div class="stats-number text-warning">{{ low_stock_count }}</div>
                            <div class="stats-label">库存不足</div>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card stats-card h-100">
                        <div class="card-body text-center">
                            <div class="stats-number text-danger">{{ out_of_stock_count }}</div>
                            <div class="stats-label">已缺货</div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 筛选区域 -->
            <div class="filter-section">
                <form method="get" class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label">库存状态</label>
                        <select name="status" class="form-select">
                            <option value="all" {% if selected_status == 'all' %}selected{% endif %}>全部状态</option>
                            <option value="normal" {% if selected_status == 'normal' %}selected{% endif %}>正常库存</option>
                            <option value="reorder" {% if selected_status == 'reorder' %}selected{% endif %}>需要补货</option>
                            <option value="low_stock" {% if selected_status == 'low_stock' %}selected{% endif %}>库存不足</option>
                            <option value="out_of_stock" {% if selected_status == 'out_of_stock' %}selected{% endif %}>已缺货</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">仓库</label>
                        <select name="warehouse" class="form-select">
                            <option value="">全部仓库</option>
                            {% for warehouse in warehouses %}
                                <option value="{{ warehouse.id }}" 
                                        {% if selected_warehouse == warehouse.id|stringformat:"s" %}selected{% endif %}>
                                    {{ warehouse.name }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">类别</label>
                        <select name="category" class="form-select">
                            <option value="">全部类别</option>
                            {% for category in categories %}
                                <option value="{{ category.id }}" 
                                        {% if selected_category == category.id|stringformat:"s" %}selected{% endif %}>
                                    {{ category.name }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">&nbsp;</label>
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fas fa-filter me-1"></i>筛选
                        </button>
                    </div>
                </form>
            </div>

            <!-- 耗材列表 -->
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="fas fa-list me-2"></i>耗材库存状态</h5>
                    <small class="text-muted">共 {{ total_count }} 条记录</small>
                </div>
                <div class="card-body p-0">
                    {% if page_obj %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>耗材信息</th>
                                    <th>当前库存</th>
                                    <th>库存状态</th>
                                    <th>库存比例</th>
                                    <th>仓库位置</th>
                                    <th>供应商</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for consumable in page_obj %}
                                <tr>
                                    <td>
                                        <div>
                                            <strong>{{ consumable.name }}</strong>
                                            <br>
                                            <small class="text-muted">{{ consumable.code }} - {{ consumable.category.name|default:"-" }}</small>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="text-center">
                                            <span class="fw-bold 
                                                {% if consumable.get_current_alert_level == 'normal' %}text-success
                                                {% elif consumable.get_current_alert_level == 'reorder_point' %}text-info
                                                {% elif consumable.get_current_alert_level == 'low_stock' %}text-warning
                                                {% elif consumable.get_current_alert_level == 'out_of_stock' %}text-danger
                                                {% endif %}">
                                                {{ consumable.quantity }}
                                            </span>
                                            <br>
                                            <small class="text-muted">最小: {{ consumable.min_quantity }}</small>
                                        </div>
                                    </td>
                                    <td>
                                        {% if consumable.get_current_alert_level == 'normal' %}
                                            <span class="badge bg-success status-badge">正常</span>
                                        {% elif consumable.get_current_alert_level == 'reorder_point' %}
                                            <span class="badge bg-info status-badge">需补货</span>
                                        {% elif consumable.get_current_alert_level == 'low_stock' %}
                                            <span class="badge bg-warning status-badge">库存不足</span>
                                        {% elif consumable.get_current_alert_level == 'out_of_stock' %}
                                            <span class="badge bg-danger status-badge">已缺货</span>
                                        {% else %}
                                            <span class="badge bg-secondary status-badge">未知</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <div>
                                            <div class="d-flex justify-content-between mb-1">
                                                <small>{{ consumable.get_stock_percentage|default:0 }}%</small>
                                            </div>
                                            <div class="stock-bar">
                                                <div class="stock-fill 
                                                    {% if consumable.get_stock_percentage <= 10 %}bg-danger
                                                    {% elif consumable.get_stock_percentage <= 25 %}bg-warning
                                                    {% elif consumable.get_stock_percentage <= 50 %}bg-info
                                                    {% else %}bg-success{% endif %}" 
                                                    style="width: {{ consumable.get_stock_percentage|default:0 }}%">
                                                </div>
                                            </div>
                                        </div>
                                    </td>
                                    <td>{{ consumable.warehouse.name|default:"-" }}</td>
                                    <td>
                                        {% if consumable.supplier %}
                                            <div>
                                                {{ consumable.supplier.name }}
                                                {% if consumable.supplier.contact_person %}
                                                <br><small class="text-muted">{{ consumable.supplier.contact_person }}</small>
                                                {% endif %}
                                            </div>
                                        {% else %}
                                            -
                                        {% endif %}
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                        <p class="text-muted">暂无符合条件的耗材记录</p>
                        {% if selected_status != 'all' or selected_warehouse or selected_category %}
                        <a href="{% url 'assets:inventory_status_overview' %}" class="btn btn-outline-primary">
                            <i class="fas fa-times me-1"></i>清除筛选条件
                        </a>
                        {% endif %}
                    </div>
                    {% endif %}
                </div>
            </div>

            <!-- 分页 -->
            {% if page_obj.has_other_pages %}
            <nav aria-label="分页导航" class="mt-4">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page=1{% if selected_status != 'all' %}&status={{ selected_status }}{% endif %}{% if selected_warehouse %}&warehouse={{ selected_warehouse }}{% endif %}{% if selected_category %}&category={{ selected_category }}{% endif %}">首页</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if selected_status != 'all' %}&status={{ selected_status }}{% endif %}{% if selected_warehouse %}&warehouse={{ selected_warehouse }}{% endif %}{% if selected_category %}&category={{ selected_category }}{% endif %}">上一页</a>
                        </li>
                    {% endif %}

                    {% for num in page_obj.paginator.page_range %}
                        {% if page_obj.number == num %}
                            <li class="page-item active">
                                <span class="page-link">{{ num }}</span>
                            </li>
                        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ num }}{% if selected_status != 'all' %}&status={{ selected_status }}{% endif %}{% if selected_warehouse %}&warehouse={{ selected_warehouse }}{% endif %}{% if selected_category %}&category={{ selected_category }}{% endif %}">{{ num }}</a>
                            </li>
                        {% endif %}
                    {% endfor %}

                    {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if selected_status != 'all' %}&status={{ selected_status }}{% endif %}{% if selected_warehouse %}&warehouse={{ selected_warehouse }}{% endif %}{% if selected_category %}&category={{ selected_category }}{% endif %}">下一页</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if selected_status != 'all' %}&status={{ selected_status }}{% endif %}{% if selected_warehouse %}&warehouse={{ selected_warehouse }}{% endif %}{% if selected_category %}&category={{ selected_category }}{% endif %}">末页</a>
                        </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
// 自动提交表单当选择框改变时
document.addEventListener('DOMContentLoaded', function() {
    const selects = document.querySelectorAll('select[name="status"], select[name="warehouse"], select[name="category"]');
    selects.forEach(select => {
        select.addEventListener('change', function() {
            this.form.submit();
        });
    });
});
</script>
{% endblock %}
